#include "xbench/benchmark.hpp"

using namespace std;

vector<xbench::State> xbench::Benchmark::_states{};
vector<size_t> xbench::Benchmark::_name_hash{};

xbench::State &xbench::Benchmark::get_state(const string &name) {
    size_t name_hash = hash<string>{}(name);
    for(size_t i = 0, length = xbench::Benchmark::_name_hash.size(); i < length; i++){
        if(name_hash == xbench::Benchmark::_name_hash[i] && name == xbench::Benchmark::_states[i].name()){
            // 找到state了
            return xbench::Benchmark::_states[i];
        }
    }
    // 没找到state，新建一个
    xbench::Benchmark::_states.emplace_back(name);
    xbench::Benchmark::_name_hash.push_back(name_hash);
    return xbench::Benchmark::_states.back();
}

xbench::StateTable xbench::Benchmark::table() {
    xbench::StateTable result;
    for(const auto& state: xbench::Benchmark::_states){
        result.add_state(state);
    }
    return result;
}


std::ostream &xbench::operator<<(ostream &os, const xbench::Benchmark &benchmark) {
    xbench::StateTable tb = xbench::Benchmark::table();
    os << ColorPrint(tb);
    return os;
}





